package com.linxianqin.springbootVue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linxianqin.springbootVue.domain.constant.UserConstants;
import com.linxianqin.springbootVue.domain.convert.SysPostConvert;
import com.linxianqin.springbootVue.domain.dto.SysPostDTO;
import com.linxianqin.springbootVue.domain.entity.SysPost;
import com.linxianqin.springbootVue.domain.query.SysPostQuery;
import com.linxianqin.springbootVue.mapper.SysPostMapper;
import com.linxianqin.springbootVue.service.SysPostService;
import com.linxianqin.springbootVue.utils.text.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 岗位信息表 服务实现类
 * </p>
 *
 * @author lxq
 * @since 2020-12-16
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {

    @Override
    public Page<SysPostDTO> selectPostPage(Page<SysPostDTO> page, SysPostQuery query) {
        Page<SysPost> entityPage = SysPostConvert.INSTANCE.dto2entity(page);
        LambdaQueryWrapper<SysPost> queryWrapper=new QueryWrapper<SysPost>().lambda();
        if (StringUtils.isNotNull(query.getStatus())){
            queryWrapper.eq(SysPost::getStatus,query.getStatus());
        }
        if (StringUtils.isNotNull(query.getPostCode())){
            queryWrapper.like(SysPost::getPostCode,query.getPostCode());
        }
        if (StringUtils.isNotNull(query.getPostName())){
            queryWrapper.like(SysPost::getPostName,query.getPostName());
        }
        Page<SysPost> selectPage = baseMapper.selectPage(entityPage, queryWrapper);
        return SysPostConvert.INSTANCE.entity2dto(selectPage);
    }
    /**
    * 校验岗位名称
    *
    * @param postDTO 岗位信息
    * @return 结果
    */
    @Override
    public String checkPostNameUnique(SysPostDTO postDTO) {
        Long postId = StringUtils.isNull(postDTO.getPostId()) ? -1L : postDTO.getPostId();
        SysPost sysPost = baseMapper.selectById(postId);
        if (StringUtils.isNotNull(sysPost) && sysPost.getPostId().longValue() != postId.longValue()) {
          return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkPostCodeUnique(SysPostDTO postDTO) {
        Long postId = StringUtils.isNull(postDTO.getPostId()) ? -1L : postDTO.getPostId();
        LambdaQueryWrapper<SysPost> queryWrapper = new QueryWrapper<SysPost>().lambda();
        queryWrapper.eq(SysPost::getPostCode,postDTO.getPostCode());
        SysPost sysPost = baseMapper.selectOne(queryWrapper);
        if (StringUtils.isNotNull(sysPost) && sysPost.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    @Override
    public List<String> selectPostListByUserId(Long userId) {
        return baseMapper.selectPostListByUserId(userId);
    }
    /**
     * 查询用户所属岗位组
     *
     * @param userName 用户名
     * @return 结果
     */
    @Override
    public List<SysPostDTO> selectPostsByUserName(String userName) {
        return SysPostConvert.INSTANCE.entity2dto(baseMapper.selectPostsByUserName(userName));
    }

}
